#ifndef MUSICCOLLECTEDDAO_H
#define MUSICCOLLECTEDDAO_H

#include <QList>
#include <QString>
#include <QSqlDatabase>
#include <QSqlError>

// 歌曲数据模型
struct MusicInfo {
    QString id;         // 歌曲ID
    QString title;      // 歌名
    QString singer;     // 歌手
    QString url;        // 音乐URL
    bool isCollected;   // 是否收藏
    
    // 添加比较运算符重载
    bool operator==(const MusicInfo &other) const {
        return this->id == other.id;  // 通常只需要比较ID是否相同
    }
};

// 音乐收藏数据访问对象接口
class MusicCollectedDao {
public:
    virtual ~MusicCollectedDao() = default;
    
    // 数据库连接管理
    virtual bool openConnection() = 0;
    virtual void closeConnection() = 0;
    virtual bool isConnected() const = 0;
    virtual QString lastError() const = 0;
    
    // 收藏管理
    virtual bool addToCollection(const MusicInfo &music) = 0;
    virtual bool removeFromCollection(const QString &musicId) = 0;
    virtual bool updateCollectionStatus(const QString &musicId, bool isCollected) = 0;
    virtual bool isMusicCollected(const QString &musicId) const = 0;
    
    // 查询操作
    virtual QList<MusicInfo> getAllCollectedMusic() const = 0;
    virtual MusicInfo getMusicById(const QString &musicId) const = 0;
};

// MySQL实现
class MySqlMusicCollectedDao : public MusicCollectedDao {
public:
    MySqlMusicCollectedDao(const QString &host, const QString &dbName, 
                           const QString &user, const QString &password);
    ~MySqlMusicCollectedDao() override;
    
    // 数据库连接管理
    bool openConnection() override;
    void closeConnection() override;
    bool isConnected() const override;
    QString lastError() const override;
    
    // 收藏管理
    bool addToCollection(const MusicInfo &music) override;
    bool removeFromCollection(const QString &musicId) override;
    bool updateCollectionStatus(const QString &musicId, bool isCollected) override;
    bool isMusicCollected(const QString &musicId) const override;
    
    // 查询操作
    QList<MusicInfo> getAllCollectedMusic() const override;
    MusicInfo getMusicById(const QString &musicId) const override;
    
private:
    QString hostName;
    QString databaseName;
    QString userName;
    QString password;
    mutable QString errorMessage;
    QSqlDatabase db;
};

#endif // MUSICCOLLECTEDDAO_H
